package com.qlslylq.ad.sdk.util;

/**
 * 调用栈工具<br/>
 */
public class StackTraceUtils {

    /**
     * 获取当前调用栈<br/>
     */
    public static String here() {
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        StringBuffer sb = new StringBuffer();
        for (int index = 0; index < elements.length; index++) {
            sb.append("stack[" + index + "]：at " + elements[index] + "\n");
        }
        return sb.toString();
    }

    /**
     * 获取当前调用栈<br/>
     */
    public static String here(StackTraceElement[] elements) {
        StringBuffer sb = new StringBuffer();
        for (int index = 0; index < elements.length; index++) {
            sb.append("stack[" + index + "]：at " + elements[index] + "\n");
        }
        return sb.toString();
    }

    /**
     * 获取调用栈元素<br/>
     */
    public static StackTraceElement getStackTraceElement(int index) {
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        if (index < elements.length) {
            return elements[index];
        } else {
            return null;
        }
    }

    /**
     * 输出网络请求调用栈<br/>
     */
    public static void outputNetRequestStackTrace(int reqType) {
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        for (int index = 0; index < elements.length; index++) {
            Log.d("输出", "[" + reqType + "]" + "请求栈[" + index + "]：at " + elements[index]);
        }
    }

    /**
     * 获取调用栈元素<br/>
     */
    public static StackTraceElement getActivityStackTraceElement() {
        return getStackTraceElement(5);
    }

    /**
     * 获取代码行号<br/>
     */
    public static int getCodeLine() {
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        if (elements.length >= 4) {
            return elements[3].getLineNumber();
        }
        return 1;
    }

}
